#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>

std::istream & f(std::istream &in)
{
    std::string v;
    while(in >> v, !in.eof()) // 遇到文件结束符才停止读取
    {
        if(in.bad())   
        {
            throw std::runtime_error("IO stream error");
        }
        if(in.fail())
        {
            std::cerr << "Data error, please try again: " << std::endl;
            in.clear();
            in.ignore(100, '\n');
            continue;
        }
        std::cout << v << std::endl;
    }
    in.clear();
    return in;
}

int main()
{
    std::ostringstream msg;
    msg << "C++ primer 5th " << std::endl;
    std::istringstream in(msg.str());
    f(in);
    f(std::cin);
    return 0;
}